package com.common.controller.user;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.entity.Message;
import com.common.entity.User;
import com.common.entity.UserCorrelationRole;
import com.common.service.RoleService;
import com.common.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Resource
	private UserService userService;

	@Resource
	private RoleService roleService;
	
	@RequestMapping(value="user",method=RequestMethod.GET)
	public String getUser(Model model){
		model.addAttribute("userList", userService.getAllUserList());
		model.addAttribute("roleList", roleService.getRolesList());
		return "permission/user";
	}
	
	@ResponseBody
	@RequestMapping(value="addRole",method=RequestMethod.POST)
	public Message addPermission(String username,Integer role){
		User user = new User(username);
		user = userService.findUserByName(user);
		UserCorrelationRole userCorrelationRole = new UserCorrelationRole(user.getId(), role);
		userService.correlationRole(userCorrelationRole);
		Message message = new Message();
		message.setMessage("添加成功");
		return message;
	}
	
	@ResponseBody
	@RequestMapping(value="deleteRole",method=RequestMethod.POST)
	public Message deletePermission(String username,Integer role){
		User user = new User(username);
		user = userService.findUserByName(user);
		UserCorrelationRole userCorrelationRole = new UserCorrelationRole(user.getId(), role);
		userService.uncorrelationRole(userCorrelationRole);
		Message message = new Message();
		message.setMessage("删除成功");
		return message;
	}
}
